
package com.example.maybeaccount.Entry;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

import com.example.maybeaccount.Mode.Account;
import com.example.maybeaccount.Mode.AsteriskPasswordTrans;
import com.example.maybeaccount.Mode.ActivityController;
import com.example.maybeaccount.Mode.EncrypDES;
import com.example.maybeaccount.Mode.SqliteDB;
import com.example.maybeaccount.R;


/**
 * @author whb
 * 文本密码登录
 */
public class LoginActivity extends AppCompatActivity {
    private Button reg;
    private Button login;
    private EditText count;
    private EditText pwd;
    private TextView state;
    private List<User> userList;
    private List<User> dataList = new ArrayList<>();
    private TextView textOthers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        login = (Button) findViewById(R.id.login);
        count = (EditText) findViewById(R.id.count);
        pwd   = (EditText) findViewById(R.id.pwd);
        pwd.setTransformationMethod(new AsteriskPasswordTrans());

        textOthers = findViewById(R.id.others);
        textOthers.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this,Entrance.class);
                startActivity(intent);
            }
        });

        userList = SqliteDB.getInstance(getApplicationContext()).loadUser();
        try{
            EncrypDES encrypDES = new EncrypDES();
            count.setText(encrypDES.decrypt(userList.get(0).getUsername()));
        }catch (Exception e){
            Log.d("错误",e.getMessage());
        }


        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name=count.getText().toString().trim();
                String pass=pwd.getText().toString().trim();
                try {
                    EncrypDES encrypDES = new EncrypDES();
                    int result=SqliteDB.getInstance(getApplicationContext()).Quer(encrypDES.double_encrypt(pass),encrypDES.encrypt(name));
                    if (result==1)
                    {
                        Toast.makeText(getApplicationContext(),"登录成功",Toast.LENGTH_LONG).show();
                        ActivityController.resumeActivity(LoginActivity.this,LoginActivity.this);
                    }
                    else if (result==0){
                        Toast.makeText(getApplicationContext(),"用户名不存在",Toast.LENGTH_LONG).show();
                        count.setText("");
                        pwd.setText("");

                    }
                    else if(result==-1)
                    {
                        Toast.makeText(getApplicationContext(),"密码错误",Toast.LENGTH_LONG).show();
                        pwd.setText("");
                    }
                }catch (Exception e){
                    Log.d("错误",e.getMessage());
                }
                //userList = SqliteDB.getInstance(getApplicationContext()).loadUser();


            }
        });
    }

    /**
     * 重写返回键 直接退出程序
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode== android.view.KeyEvent.KEYCODE_BACK){
            Intent home = new Intent(Intent.ACTION_MAIN);
            home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            home.addCategory(Intent.CATEGORY_HOME);
            startActivity(home);
        }
        return true;
    }
}